package m05;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;

import m06.GameScreen;
import m07.GamePanel;
import m07.Player;
import m09.Global;

/**
 * This is our configuration screen class.  
 * It handles the configuration screen.
 * @author jessica521
 *
 */
public class ConfigScreen extends JFrame{
	private static final long serialVersionUID = 5630644077128211575L;
	private JPanel contentPane;
	private JTextField textField;
	private JSpinner spinner;
	private JSpinner spinner_1;
	private JSpinner spinner_2;
	private JSpinner spinner_3;
	private JLabel label_1;
	private int spinnerVal, spinner1Val, spinner2Val, spinner3Val;
	private int curVal = 16;
	private JRadioButton rdbtnHard, rdbtnEasy;
	private Icon shipIcon;
	
	private static GameScreen gameFrame;// = new GameScreen();

	/**
	 * Create the frame. Constructor
	 */
	public ConfigScreen() {
		
		BufferedImage shipImg;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("ship.png");
			shipImg = ImageIO.read(input);
			shipIcon = new ImageIcon(shipImg);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Icon Not Loaded");
		}
		
		setAlwaysOnTop(true);
		setResizable(false);
		setTitle("New Player");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 321);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		

		textField = new JTextField();
		textField.setBounds(150, 40, 212, 28);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblPlayerName = new JLabel("Player Name:");
		lblPlayerName.setForeground(Color.white);
		lblPlayerName.setBounds(44, 46, 105, 16);
		contentPane.add(lblPlayerName);

		spinnerVal = 0;
		spinner1Val = 0;
		spinner2Val = 0;
		spinner3Val = 0;

		spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(spinnerVal, 0, 16, 1));
		spinner.setBounds(150, 100, 53, 28);
		spinner.setForeground(Color.white);
		spinner.addChangeListener(new MyListener());
		contentPane.add(spinner);

		spinner_1 = new JSpinner();
		spinner_1.setModel(new SpinnerNumberModel(spinner1Val, 0, 16, 1));
		spinner_1.setBounds(150, 139, 53, 28);
		spinner_1.setForeground(Color.white);
		spinner_1.addChangeListener(new MyListener());
		contentPane.add(spinner_1);

		spinner_2 = new JSpinner();
		spinner_2.setModel(new SpinnerNumberModel(spinner2Val, 0, 16, 1));
		spinner_2.setBounds(150, 179, 53, 28);
		spinner_2.setForeground(Color.white);
		spinner_2.addChangeListener(new MyListener());
		contentPane.add(spinner_2);

		spinner_3 = new JSpinner();
		spinner_3.setModel(new SpinnerNumberModel(spinner3Val, 0, 16, 1));
		spinner_3.setBounds(150, 216, 53, 28);
		spinner_3.setForeground(Color.white);
		spinner_3.addChangeListener(new MyListener());
		contentPane.add(spinner_3);

		JButton btnDone = new JButton("Done");
		btnDone.setBounds(303, 216, 105, 29);
		btnDone.addActionListener(new DoneListener());
		contentPane.add(btnDone);

		JLabel lblPilot = new JLabel("Fighter:");
		lblPilot.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPilot.setBounds(80, 147, 61, 16);
		lblPilot.setForeground(Color.white);
		contentPane.add(lblPilot);

		JLabel label = new JLabel("Pilot:");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(80, 108, 61, 16);
		label.setForeground(Color.white);
		contentPane.add(label);

		JLabel lblTrader = new JLabel("Trader:");
		lblTrader.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTrader.setBounds(80, 187, 61, 16);
		lblTrader.setForeground(Color.white);
		contentPane.add(lblTrader);

		JLabel lblEngineer = new JLabel("Engineer:");
		lblEngineer.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEngineer.setBounds(80, 224, 61, 16);
		lblEngineer.setForeground(Color.white);
		contentPane.add(lblEngineer);

		label_1 = new JLabel("You have 16 credits left");
		label_1.setBounds(215, 100, 218, 16);
		label_1.setForeground(Color.white);
		contentPane.add(label_1);

		ButtonGroup group = new ButtonGroup();

		rdbtnEasy = new JRadioButton("Easy");
		rdbtnEasy.setBounds(255, 139, 141, 23);
		group.add(rdbtnEasy);
		rdbtnEasy.setSelected(true);
		rdbtnEasy.setForeground(Color.white);
		contentPane.add(rdbtnEasy);

		rdbtnHard = new JRadioButton("Hard");
		rdbtnHard.setBounds(255, 167, 141, 23);
		rdbtnHard.setForeground(Color.white);
		group.add(rdbtnHard);
		contentPane.add(rdbtnHard);
		
		BufferedImage myPicture;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("config_screen.png");
			myPicture = ImageIO.read(input);
			JLabel picLabel = new JLabel(new ImageIcon(myPicture));
			picLabel.setBounds(0, 0, 450, 300);
			contentPane.add(picLabel);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Background Not Loaded");
		}
	}


	/**
	 * This is the listener for the spinners.
	 */
	private class MyListener implements ChangeListener
	{
		/**
		 * This listens to the state of the spinners.
		 * @param the event that it listens to.
		 */
		@Override
		public void stateChanged(ChangeEvent event) {
			try {
				spinner.commitEdit();
				spinner_1.commitEdit();
				spinner_2.commitEdit();
				spinner_3.commitEdit();
				curVal = 16 - (Integer)spinner.getValue()-(Integer)spinner_1.getValue()-(Integer)spinner_2.getValue()-(Integer)spinner_3.getValue();
				label_1.setText("You have "+curVal + " credits left.");
				int x = ((Integer)spinner.getValue()).intValue();
				int x1 = ((Integer)spinner_1.getValue()).intValue();
				int x2 = ((Integer)spinner_2.getValue()).intValue();
				int x3 = ((Integer)spinner_3.getValue()).intValue();
				spinner.setModel(new SpinnerNumberModel(x, 0, 16-(Integer)spinner_1.getValue()-(Integer)spinner_2.getValue()-(Integer)spinner_3.getValue(), 1));
				spinner_1.setModel(new SpinnerNumberModel(x1, 0, 16-(Integer)spinner.getValue()-(Integer)spinner_2.getValue()-(Integer)spinner_3.getValue(), 1));
				spinner_2.setModel(new SpinnerNumberModel(x2, 0, 16-(Integer)spinner.getValue()-(Integer)spinner_1.getValue()-(Integer)spinner_3.getValue(), 1));
				spinner_3.setModel(new SpinnerNumberModel(x3, 0, 16-(Integer)spinner_1.getValue()-(Integer)spinner_2.getValue()-(Integer)spinner.getValue(), 1));


			} catch (ParseException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * The listener for the done button.
	 * @author jessica521
	 *
	 */
	private class DoneListener implements ActionListener
	{
		/**
		 * listens to the actions performed by the done button.
		 * @param the event that the listener hears.
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (curVal != 0){
				JOptionPane.showMessageDialog(null, "You still have " + curVal + " credits left!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
				//JOptionPane.showMessageDialog(null, "You still have " + curVal + " credits left!");	
			}
			else if (textField.getText().equals("")){
				JOptionPane.showMessageDialog(null, "You never entered your name!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
				//JOptionPane.showMessageDialog(null, "You never entered your name!");
			}
			else {
				//System.out.println("Your name is " + textField.getText());
				//System.out.println("Pilot points: " + spinner.getValue());
				//System.out.println("Fighter points: " + spinner_1.getValue());
				//System.out.println("Trader points: " + spinner_2.getValue());
				//System.out.println("Engineer points: " + spinner_3.getValue());
				Global.currentPlayer= new Player (textField.getText(), (Integer)spinner.getValue(), (Integer)spinner_1.getValue(), (Integer)spinner_2.getValue(), (Integer)spinner_3.getValue());
				
				if (rdbtnEasy.isSelected()){
					//System.out.println("You are on easy mode.");
				}
				else {
					//System.out.println("You are on hard mode.");
				}
				dispose();
				GamePanel gp = new GamePanel();
				gp.updatePlayer();
				gameFrame = new GameScreen(gp);
				gameFrame.setVisible(true);
			}
		}
	}
}
